<?php

/**
 *
 * 系统配置功能（废弃 可以删除）
 * @package
 * @author
 */

namespace app\api\controller;

use think\Validate;
use think\Db;

class System //extends Common
{
    protected $rules = [
        'ip' => 'require|ip',
        'mask' => 'require|ip',
        'gateway' => 'require|ip',
        'dns' => 'require|ip',
    ];
    //
    // public function __constract()
    // {
    //     parent::_initialize();
    //     //检查用户 如果用户不是超级管理员则不执行
    //     if ($GLOBALS['userInfo']['id'] != 1) { 
    //         return resultArray(['error' => '非管理员', 'code' => '106']);
    //     }
    // }
    //现在的ip
    public function presentIP()
    {
        try {
            //从数据库获取当前ip及各参数
            $ip_result = DB::name('ip')->where(['id' => 1])->find();
            $data = [
                'ip' => $ip_result['ip'],
                'mask' => $ip_result['mask'],
                'gateway' => $ip_result['gateway'],
                'dns' => $ip_result['dns'],
            ];
            return resultArray(['data' => $data]);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    //修改ip
    public function changeIP()
    {
        return 'haha';
        try {
            //先判断能否修改ip 
            //如果有正在转写的内容则不能修改
            $map = [
                'end_time' => 0,
                'transition_state' => 0,
                'is_delete' => 0,
                'start_time' => ['>', time() - 60 * 60 * 24]
            ];
            $real_time_count = DB::name('meeting_live_speech')->where($map)->count();
            if ($real_time_count) {
                return resultArray(['error' => '实时转写正在进行，不能修改ip']);
            }
            $offline_cmd  = "top -b -n 1 |grep nnet3-lat|cut -c 46";
            exec($offline_cmd, $off_re);
            if ($off_re) {
                return resultArray(['error' => '非实时转写正在进行，不能修改ip']);
            }
            $param = input('param.');
            //先进行validate验证
            $validate = new Validate($this->rules);
            if (!$validate->check($param)) {
                return resultArray(['error' => '修改ip格式不正确']);
            }
            //开始提交
            $ip = DB::name('ip')->where(['id' => 1])->find()['ip'];
            //修改地址的链接
            $url = "http://" . $ip . ":10080/?request=changeServerInfo&ip=" . $param['ip'] . "&mask=" . $param['mask'] . "&gateway=" . $param['gateway'] . "&dns=" . $param['dns'];
            //调用修改ip接口
            parse_str(file_get_contents($url), $requestData);
            print_r($requestData);

            //休眠几秒为后台修改ip等待
            sleep(5);
            //检查是否已经修改ip
            exec("curl --connect-timeout 1 http://" . $param['ip'] . ":10080/huisheng_api/api/test/testServer", $r);
            //exec("curl --connect-timeout 1 http://192.168.0.75:10080/huisheng_api/api/test/testServer", $r);
            if (empty($r)) {
                return resultArray(['error' => '修改失败,请稍后尝试。']);
            }
            //调用接口将新IP传给网关
            $softlinkurl = "http://" . $param['ip'] . ":10080/?request=changeSoftLink&softLink=http://" . $param['ip'] . "/data/";

            //parse_str(file_get_contents($url), $requestData);  
            //print_r($requestData);
            //储存新ip
            $ipdata = [
                'ip' => $param['ip'],
                'mask' => $param['mask'],
                'gateway' => $param['gateway'],
                'dns' => $param['dns'],
                'update_time' => time()
            ];
            $res = DB::name('ip')->where(['id' => 1])->update($ipdata);
            //没问题就可以返回了 耶！
            return resultArray(['data' => '']);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    public function boxWaveJpeg($uid, $status)
    {
        try {
            $user = Model('User')->get($uid);
            if (!$user) {
                return resultArray(['error' => '未找到用户']);
            }
            switch($status){
                case '1':
                case '2':
                    $user->save(['post_id' => $status]);
                    return resultArray(['data' => '']);
                default:
                return resultArray(['error' => '参数错误']);
            }
        } catch (\Exception $e) {
            return resultArray(['error' => '请求错误']);
        }
    }

    public function getWaveJpeg($uid){
        try {
            $user = Model('User')->get($uid);
            if (!$user) {
                return resultArray(['error' => '未找到用户']);
            }
            $result = [
                'status' => $user->post_id
            ];
            return resultArray(['data' => $result]);
        } catch (\Exception $e) {
            return resultArray(['error' => '请求错误']);
        }
    }
}
